<template>
  <el-dialog
    title="新增员工"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <!-- 表单 -->
    <el-form ref="addEmploy" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择">
          <!-- <el-option label="正式" :value="1" />
          <el-option label="非正式" :value="2" />hireType -->
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="getDepartments" />
        <!-- [{ name: '行政部', manager: '刘备' }] -->
        <el-tree
          v-show="isShow"
          v-loading="loading"
          style="width:80%"
          :data="treeData"
          :props="{label: 'name'}"
          default-expand-all
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="handleClose">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
//  实现弹窗组件的显示和隐藏
//  其实 el-dialog 组件显示 隐藏 visible
//  父到子 props
//  点击关闭按钮 close $emit 去改父组件里的值 (upate:dialogVisible)
//  handleClose 完善 // 1. 表单重置 2.表单绑定的值从新赋值

import employeesEnum from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'

export default {
  name: 'HrsaasAddEmployee',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      hireType: employeesEnum.hireType,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验 步骤
      // el-form配置model和rules属性
      // el-form-item配置prop属性
      // 表单进行v-model双向绑定

      // 表单校验
      // 需求：
      // 用户名必填，username，长度为1-4位
      // 手机号必填，mobile，需满足 正则表达式 /^1[3-9]\d{9}$/
      // 聘用形式必填， formOfEmployment change
      // 工号必填，workNumber
      // 部门必填，departmentName change
      // 入职时间必填， timeOfEntry
      // 转正时间可选，correctionTime
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: 'mobile 不满足格式', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式必填' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间必填' }
        ],
        workNumber: [
          { required: true, message: '工号必填', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门必填' }
        ]
      },
      treeData: [{ name: '行政部', manager: '刘备' }],
      isShow: false,
      loading: false
    }
  },
  // 部门处理
  // 部门这一块 el-input + el-tree 组成
  // 完成这一块的渲染 假数据：[{ name: '行政部', manager: '刘备' }]
  // 获取组织架构的数据
  // 什么时候 调用组织架构的接口 获得焦点
  methods: {
    handleClose() {
      // 表单重置
      this.$refs.addEmploy.resetFields()
      // 重新赋值
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.isShow = false
      this.$emit('update:dialogVisible', false)
    },
    async getDepartments() {
      this.isShow = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
      // [{}, {}, {}] ==> []
      // el-tree loading状态
    },
    selectNode(node) {
      // 选中树的节点 把节点对应的数据 给 departmentName这个字段
      this.formData.departmentName = node.name
      this.isShow = false
      // 细节优化
      // el-tree 显示隐藏 问题
    },
    // 点击确定待用接口完成新增功能
    // 1. 接口封装 复制
    // 2. 确定按钮 点击事件  form校验通过之后 调用接口 引入接口
    // 3. 调用接口 成功 弹窗关闭 父组件 里列表刷新
    async btnOk() {
      // 表单校验通过
      try {
        await this.$refs.addEmploy.validate()
        // 调用接口
        await addEmployee(this.formData)
        // 关掉弹窗
        this.handleClose()
        // 需要调用父组件里 获取列表的方法
        this.$parent.getEmployeeList()
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
